package com.sas.controller;


import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sas.entity.Resources;
import com.sas.entity.User;
import com.sas.service.ResourcesService;
import com.sas.service.UserService;




@Controller
@RequestMapping("user.do")
public class UserfController {
	
    private String msgc=null;
	@Autowired
	UserService userService;
	@Autowired
	ResourcesService resourcesService;
	
	private  static Logger log = Logger.getLogger(UserfController.class);
	/**
	 * 账户登陆
	 * 
	 * @return
	 */
	@RequestMapping(params="method=denglu")
	public String login(String login_name, String login_passwd, HttpSession session) {
          System.out.println("进入登录");
		User user=new User(login_name,login_passwd);
		try {
			//得到登录账户
			User login_user = userService.select_user_bypass(user);
			 System.out.println("进入登录"+login_user.getUser_type()+login_user.getName());
			if (login_user != null
					&& login_user.getLogin_name().equals(login_name)
					&& login_user.getLogin_passwd().equals(
							login_passwd)) {
				
				List<Resources> select_resources_by_userid = resourcesService.select_resources_by_userid(login_user);
				
				
				System.out.println("//1"+select_resources_by_userid.toString());
				
			/*	
				int count=0;
				Map<String,List<Mulu>> map =new HashMap<String,List<Mulu>>();
				List results = new ArrayList();
					for(Resources ss :select_resources_by_userid){
						
						
						//Mulu e = new Mulu();
						//e.setRes_name(ss.getRes_name());
						String key=ss.getRes_name();
						//results.add(ss.getRes_name());
						//System.out.println("erji"+count++ +"//"+e.toString());
						
						for(Resources_er dd :ss.getResourcesLister()){
							Mulu e1 = new Mulu();
							e1.setEj_name(dd.getEj_name());
							e1.setUrl(dd.getUrl());							
							
							results.add(e1);
							map.put(key, results);
							System.out.println("erjixia"+count++ +"//"+map.toString());
						};
						
					};
					
					System.out.println("map"+map.toString());*/
				
				//System.out.println(results.toString());
				
				//session.setAttribute("mulu", map);
				
				session.setAttribute("mulu2", select_resources_by_userid);
				session.setAttribute("user", login_user);
				return "welcomeAction";
			}
			session.setAttribute("msgc", "用户名或密码不正确");
			
		} catch (Exception e) {
			session.setAttribute("msgc", "用户名或密码错误");
			 System.out.println("进入登录"+e);
			log.error("进入登录"+e);
		}
		
			
		
		return "login";
	}
    //根据角色重定向请求
	@RequestMapping(params="method=mains" ,value="/redirect")
	public String testRedirect(HttpSession session){
		User user=(User) session.getAttribute("user");
		user.getUser_type();
		
		if(user.getUser_type().equals("2") ){
			
			//跳转到管理员
			return "redirect:/user.do?method=guanliyuan";
		}else{
			//跳转到普通用户
			return "redirect:/user.do?method=zhiyuan";
		}

	}
	
	//进入主页
	@RequestMapping(params="method=main")
	public String main(HttpSession session){
		
		
		User user=(User) session.getAttribute("user");

		/*if(user.getResourcesList()!=null && user.getResourcesList().size()>0){
			for(Resources resources:user.getResourcesList()){
				if(resources.getRole_code()!=null){
					//first_url=resources.getRes_url();
					break;
				}
				
			}
			
		}*/
		//session.setAttribute("user", user);
		return "index_1";
	}
	//退出登录
	@RequestMapping(params="method=loginout")
	public String loginout(HttpSession session){
		System.out.println("进入退出");
			
		//清除当前session的所有相关信息
		session.invalidate();
         
	
		return "login";
	}
	

}
